//
//  ChooseViewController.m
//  LClass
//
//  Created by 严军 on 16/5/30.
//  Copyright © 2016年 YanJun. All rights reserved.
//

#import "ChooseViewController.h"
#import "ChooseView.h"
#import "QQRequestManager+handle.h"
#import "MainViewController.h"
@interface ChooseViewController ()
{
    ChooseView* chooseview;
    NSArray* courseList;
}
@end

@implementation ChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    chooseview=[[ChooseView alloc]init];
    self.view=chooseview;
    
    //请求课程列表
    [[QQRequestManager sharedRequestManager]GET_courseWithParameters:@{@"userId":[[[QQRequestManager sharedRequestManager]GET_userInfo] objectForKey:@"userId"]} success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject objectForKey:@"list"]) {
            courseList=[responseObject objectForKey:@"list"];
            chooseview.dataList=courseList;
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    if([[Utility sharedInstance]arryIsNotNull:courseList])
    {
        chooseview.dataList=courseList;
    }
    
    __block ChooseViewController* blockSelf=self;
    chooseview.parmBlock=^(NSDictionary* parDic){
        
        [blockSelf getStuList:parDic];
    };
    
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.title=@"选 择";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftItemsSupplementBackButton=NO;
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
       
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    if (CurrentVersion <= 6.1) {
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    } else {
        self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    }

}

-(void)getStuList:(NSDictionary*)parDic
{
     __block ChooseViewController* blockSelf=self;
    if ([parDic objectForKey:@"courseId"]==0) {
        [SVProgressHUD showInfoWithStatus:@"请选择课程!"];
    }else
    {
        NSDictionary* param=@{@"courseId":[parDic objectForKey:@"courseId"]};
        //根据参数请求数据
        [[QQRequestManager sharedRequestManager] GET_kaoQinDataWithParameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
            //考勤学生列表
            NSArray* stulist=[responseObject objectForKey:@"stuList"];
            int allStuNum=(int)stulist.count;
            if (allStuNum!=0) {
                NSArray* resultAry;
                //处理考勤数据
                int type = [(NSNumber*)[parDic objectForKey:@"chooseType"] intValue];
                switch (type) {
                    case 1:
                        resultAry = [blockSelf getRandomFromArry:stulist withNumber:arc4random() % allStuNum+1];
                        break;
                    case 2:
                        //全员考勤
                        resultAry=[blockSelf getRandomFromArry:stulist withNumber:allStuNum];
                        break;
                    case 3:
                        //三分之一考勤
                        resultAry = [blockSelf getRandomFromArry:stulist withNumber:allStuNum/3];
                        break;
                    case 4:
                        //二分之一考勤
                        resultAry = [blockSelf getRandomFromArry:stulist withNumber:allStuNum/2];
                        break;
                        
                    default:
                        break;
                }
                
                //请求成功后跳转到考勤界面
                NSDictionary* dataDic=@{@"allStuNum":[NSString stringWithFormat:@"%d",allStuNum],@"kaoQinNum":[NSString stringWithFormat:@"%lu",(unsigned long)resultAry.count],@"kaoQinList":resultAry};
                MainViewController* mainVC=[[MainViewController alloc]init];
                mainVC.dataDic=dataDic;
                [blockSelf.navigationController pushViewController:mainVC animated:YES];
            }else
            {
                [blockSelf qq_performSVHUDBlock:^{
                    [SVProgressHUD showInfoWithStatus:@"一个学生都没有!"];
                }];
            }
           
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            [blockSelf qq_performSVHUDBlock:^{
                [SVProgressHUD showErrorWithStatus:(NSString*)error];
            }];
            
        }];
    }

}

-(NSArray*)getRandomFromArry:(NSArray*)temp withNumber:(int)number
{
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:temp];
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    int i;

    for (i = 0; i < number ; i ++) {
        int index = arc4random() % (temp.count - i);
        [resultArray addObject:[tempArray objectAtIndex:index]];
        NSLog(@"index:%d,xx:%@",index,[tempArray objectAtIndex:index]);
        [tempArray removeObjectAtIndex:index];
    }
    if(resultArray.count==0)
    {
        [self getRandomFromArry:temp withNumber:number];
    }
    return resultArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
